package other.edan.erp.bs;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.api.annotations.*;
import com.rkhd.platform.sdk.data.model.Account;
import com.rkhd.platform.sdk.data.model.DueDiligence_User__c;
import com.rkhd.platform.sdk.data.model.InstallationInfo__c;
import com.rkhd.platform.sdk.data.model.User;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.BatchOperateResult;
import com.rkhd.platform.sdk.model.XObject;
import com.rkhd.platform.sdk.service.XoqlService;
import org.apache.commons.lang.StringUtils;
import other.edan.erp.apitool.BsClientUtils;
import other.edan.erp.utils.HCXObjectService;
import other.edan.erp.utils.ResultVo;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description 北森连接请求类
 * @Author 黎炎松
 * 2024-11-11 17:43
 */
@RestApi(baseUrl = "/edan/bs")
public class BsClientApi {
    private static final Logger LOGGER = LoggerFactory.getLogger();

    public static void main(String[] args) {
        BsClientApi bsClientApi = new BsClientApi();
        ResultVo resultVo = bsClientApi.userIsExistBs(3464311237155394L);
        System.out.println("resultVo = " + resultVo);
    }

    /**
     * 尽职调查下的股东列表判断是否在北森存在
     * @param dataId
     * @return code: 1 在北森已存在
     */
    @RestMapping(value = "/userIsExistBs", method = RequestMethod.GET)
    public ResultVo userIsExistBs(@RestQueryParam(name = "dataId") Long dataId) {
        String MSG = "尽职调查下的股东列表判断是否在北森存在 ==> ";
        try {
            LOGGER.info(MSG + "处理开始: "+ dataId);
            if (dataId == null) {
                return ResultVo.error("传入参数不可为空");
            }

            // 获取股东列表
            String sql = "select id,name,mobile__c from dueDiligence_User__c where entityType = 3335683425852063 and dueDiligence__c = " + dataId;
            List<DueDiligence_User__c> crmShareholderList = HCXObjectService.queryAll(sql);
            LOGGER.info(MSG + "crmShareholderList: "+ crmShareholderList);
            if (crmShareholderList.size() <= 0) {
                LOGGER.info(MSG + "没有股东信息无需处理");
                return new ResultVo(200, "没有股东信息无需处理");
            }

            // 查询根据手机号查询北森对应的员工id
            List<String> phoneList = crmShareholderList.stream().map(v -> v.getMobile__c()).collect(Collectors.toList());
            //phoneList = Arrays.asList("15875786968", "13841099879");
            JSONObject reqData = new JSONObject();
            reqData.put("mobiles", phoneList);
            ResultVo resultVo = BsClientUtils.sendPost(reqData, BsClientUtils.GET_USER_ID, "");
            if (resultVo.getCode() != 200) {
                LOGGER.info(MSG + "查询异常");
                return ResultVo.error("查询异常: " + resultVo.getMsg());
            }

            List<JSONObject> bsUserList = (List<JSONObject>) resultVo.getData();
            LOGGER.info(MSG + "bsUserList: " + bsUserList);
            if (bsUserList.size() <= 0) {
                LOGGER.info(MSG + "当前尽职调查下的股东用户在北森不存在");
                return new ResultVo(200, "当前尽职调查下的股东用户在北森不存在");
            }

            // 拼接提示
            List<String> nameList =new ArrayList<>();
            for (JSONObject bsUser : bsUserList) {
                String mobile = bsUser.getString("mobile");
                DueDiligence_User__c dueDiligenceUser = crmShareholderList.stream().filter(v -> Objects.equals(v.getMobile__c(), mobile)).findFirst().orElse(null);
                if (dueDiligenceUser != null) {
                    nameList.add(dueDiligenceUser.getName());
                }
            }
            String tips = String.format("当前尽职调查下的股东用户在北森已存在(%s) ", StringUtils.join(nameList, ","));
            LOGGER.info(MSG + tips);

            LOGGER.info(MSG + "处理成功");
            return new ResultVo(1, tips);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(MSG + "处理异常:" + e.getMessage());
            return ResultVo.error("处理异常:" + e.getMessage());
        }
    }



}
